// pages/details/details.js
// var WxParse = require('../../wxParse/wxParse.js')4;
import { getdetails,
  getComments,
  getCollection,
  focusUsers,
  cancelUsers,
  cancelCollection,
  addpl
} from '../../api/details'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:'',
    commentszt:false,
    detailsid:null, //收藏文章id
    title:null,
    photo:null,
    name:null,
    pubdate:null,
    content:null,
    autid:null, //关注人的id
    collected:false, //是否关注
    iscolle:'关注',
    iconcolle:'plus',
    iczt:'info',
    lodaing:false, //转圈
    coction:false, //收藏
    pl:[],
    pltotal:null,
    lastid:null,
    one:1,
    color:'',//收藏颜色
    show:false, //底部评论遮罩区
    showll:false, //
    com_id:null, // 评论id
    plshow:false,
    plhfs:null //评论回复的数据
  },
  huifupls(e){
     this.setData({
      plhfs:e.detail
     })
  },
  onUpdateCount(e){
    console.log(e);
    this.setData({
      plshow: e.detail
    })
  },
  onClose() {
    this.setData({ plshow: false });
  },
  dianji(){
    this.setData({
      show:false,
      showll:false,
      value:''
    })
  },
  dakai(){
    console.log(1);
    this.setData({
      show:true
    })
  },
  dakaill(){
    this.setData({
      showll:true
    })
  },
  huoqu(e){
     this.setData({
      com_id:e.detail
     })
  },
  async gabuplll(){
    wx.showLoading({ // 开启加载中
      title: '加载中...'
      // mask: true,//遮罩层
    });
    try{
       const res = await addpl({
             art_id:this.data.detailsid,
             content:this.data.value,
             target: this.data.com_id
           })
           if(this.data.plhfs.total_count){
            this.setData({
              showll:false,
              'plhfs.results':[res.data.new_obj,...this.data.plhfs.results],
              'plhfs.total_count':this.data.plhfs.total_count+1,
              value:''
             })
           }else{
             this.setData({
            showll:false,
            'plhfs.results':[res.data.new_obj],
            'plhfs.total_count':1,
            value:''
           })
           }
          
        wx.showToast({
         title: '评论成功',
         icon: 'success'
       })
       
    }catch(err){
      console.log(err);
    }
    
  },
  async gabupl(){
    wx.showLoading({ // 开启加载中
      title: '加载中...'
      // mask: true,//遮罩层
    });
    try{
       const res = await addpl({
             art_id:null,
             content:this.data.value,
             target: this.data.detailsid
           })
           if(this.data.pltotal){
            this.setData({
              show:false,
              pl:[res.data.new_obj,...this.data.pl],
              pltotal:this.data.pltotal+1,
              value:''
             })
           }else{
             this.setData({
            show:false,
            pl:[res.data.new_obj],
            pltotal:1,
            value:''
           })
           }
          
        wx.showToast({
         title: '评论成功',
         icon: 'success'
       })
       
    }catch(err){
      console.log(err);
    }
    
  },
  async collection(){
    if(!this.data.coction){
      wx.showLoading({ // 开启加载中
      title: '加载中...'
      // mask: true,//遮罩层
    });
    const data = {
      target: this.data.detailsid
    }
    try{
      const res= await getCollection(data)
      wx.showToast({
        title: '操作成功',
          icon: 'success'
      })
      this.setData({
            color:'rgb(50, 150, 250)',
            coction:true
          })
    }catch(err){
       console.log(err)
    }
    }else{
      wx.showLoading({ // 开启加载中
        title: '加载中...'
        // mask: true,//遮罩层
      });
      try{
        const res= await cancelCollection(this.data.detailsid)
        wx.showToast({
          title: '操作成功',
            icon: 'success'
        })
        this.setData({
              color:'',
              coction:false
            })
      }catch(err){
         console.log(err)
      }
    }
    
  },
  async holddetails(){ // 获取详情信息
    wx.showLoading({ // 开启加载中
      title: '加载中...',
      mask: true,//遮罩层
    });
     const {data} = await getdetails(this.data.detailsid)
     this.setData({
        title:data.title,
        photo:data.aut_photo,
        name:data.aut_name,
        pubdate:data.pubdate,
        content:data.content,
        pltotal:data.comm_count,
        collected:data.is_followed,
        autid:data.aut_id,
        coction:data.is_collected
     })
     if(this.data.collected){
      this.setData({
        iscolle:'已关注',
        iconcolle:'',
        iczt:'default',
      })
     }
     if(this.data.coction){
      this.setData({
        color:'rgb(50, 150, 250)'
      })
     }
     wx.hideLoading()
  },
  async guanzhu(){
    if(!this.data.collected){
      this.setData({
      lodaing:true
    })
    try{
      const res = await focusUsers({
      target:this.data.autid
    })
    this.setData({
      iscolle:'已关注',
      iconcolle:'',
      iczt:'default',
      lodaing:false,
      collected:true
    })
    }catch(err){
      console.log(err);
    }
    }else{
      this.setData({
        lodaing:true
      })
      try{
        const res = await cancelUsers(this.data.autid)
      this.setData({
        iscolle:'关注',
        iconcolle:'plus',
        iczt:'info',
        lodaing:false,
        collected:false
      })
      }catch(err){
        console.log(err);
      }
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad({id}) {
       this.setData({
        detailsid : id
       })
       
      // var content = this.data.content 5
      // var that = this 6
      // WxParse.wxParse('content', 'html', content, that,10); 7
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.holddetails()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
     if(this.data.one===1){
      const {data} = await getComments({
        type:'a',
        source:this.data.detailsid,
        limit:10,
     })
     console.log(data);
     this.setData({
      pl:[...data.results],
      pltotal:data.total_count,
      lastid:data.last_id
      })
     }else{
      const {data} = await getComments({
        type:'a',
        source:this.data.detailsid,
        offset:this.data.lastid,
        limit:10,
     })
     console.log(data);
     this.setData({
      pl:[...this.data.pl,...data.results],
      pltotal:data.total_count,
      lastid:data.last_id
      })
     }
     this.setData({
        one:2
     })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})